import { defaultPreferenceSettings, defaultOtherWorkParams, defaultUserSettings } from '@/conf';
import {
	ILayout,
	ILoginData,
	IPreferenceSettings,
	WhiteBoardInfo,
	IDefaultMediaOptions,
	ICurrentTab,
	IOtherWork,
	IBackgroundList,
	IWatermarkImageList,
	IPocSetting,
	TIgnoreLinkedKey
} from './interface';
import clog from './CLog';
import MediaStreamConfigure from './config/MediaStreamConfigure';

class Configure {
	public layout: ILayout;
	public preferenceSettings: IPreferenceSettings;
	public loginData: ILoginData;
	public pocSetting: IPocSetting;
	public serviceUrl: string;
	public currentTab: ICurrentTab;
	public defaultMediaOptions: IDefaultMediaOptions;
	public otherWork: IOtherWork;
	public userSettings: any;
	public backgroundImageList: IBackgroundList[];
	public watermarkImageList: IWatermarkImageList[];
	public backgroundID: number;
	public watermarkID: number;
	public isTransferScene: boolean;
	// public showQualityTestingResultView: boolean = true;
	public ignoreVariableLinkedKeys: TIgnoreLinkedKey[] = [];
	public managementPath: string = '';
	public language: string = 'zh';
    public awsFileUrl: string = ''; // aws文件存储，配置文件预览url
	public customerUrl: string = ''; // 客户端url
	public formUrl: string = ''; // 表单url
	public resourceOriginUrlDomain: string = ''; // 静态资源地址
	public resourceReplaceUrlDomain: string = ''; // 静态资源替换地址
	public urlMapping: { [key: string]: string } = {}; // 静态资源替换地址
	public maxUploadFileSize: number = 1024000 * 50; // 最大上传文件大小
    public publicKey: string = publicKey; // 登录公钥
	// 白板相关
	public whiteboardInfo: WhiteBoardInfo = {
		hostName: '',
		whiteboardPath: ''
	};

	constructor() {
		this.serviceUrl = 'https://ivcs-test-k8s-azure.zealcomm.cn';
		this.layout = 'full';
		this.currentTab = 'sessionService';
		this.isTransferScene = true;
		this.defaultMediaOptions = MediaStreamConfigure.defaultMediaOptions;
		this.preferenceSettings = {
			deviceConfigHidden: false,
			virtualBackgroundConfigHidden: false,
			watermarkConfigHidden: false,
			languageConfigHidden: false,
			acceptCallConfigHidden: false,
			floatAlertConfigHidden: false,
			managementMenuHidden: false,
			agentManagemenMenuHidden: false,
			userInfoHidden: false,
			chatBoxHidden: false,
			layoutConfigHidden: false,
			bizPannelHidden: false,
			qualityTestingResultHidden: false,
			isImagePreviewHidden: false,
			isCallProgressHidden: false,
			ignoreTools: [],
			headerMenus: [],
			isRecordHidden: false, // 控制显示双录按钮(坐席开启录制/停止录制)
			isNotifyCustomerMediaUpgrade: false,
			disableAudioAnswerToVideoCall: false,
		};

		this.otherWork = {
			endSessionOtherWork: 'off',
			timeoutOtherWork: 'off',
			endSessionReason: '话术整理',
			timeoutReason: ''
		};
		this.backgroundID = -1;
		this.watermarkID = -1;

		this.userSettings = [
			{
				id: 1,
				label: '视频画面设置',
				children: [
					{
						label: '添加视频背景、视频水印',
						i18nKey: 'add-virtual-background-watermark', // 添加视频背景、视频水印
						status: ''
					}
				],
				i18nKey: 'virtaul-background-setting', // 视频画面设置
				visible: true
			},

			{
				id: 2,
				label: '视频画面设置',
				children: [
					{
						label: '添加视频背景',
						i18nKey: 'add-virtual-background', // 添加视频背景
						status: ''
					}
				],
				i18nKey: 'virtaul-background-setting', // 视频画面设置
				visible: true
			},
			{
				id: 3,
				label: '系统语言设置',
				children: [
					{
						label: '添加视频背景、视频水印',
						i18nKey: 'zh-ch-tw', // 简体中文、繁体中文
						status: ''
					}
				],
				i18nKey: 'language-setting', // 系统语言设置
				visible: true
			},
			{
				id: 4,
				label: '自动小休设置',
				children: [
					// {
					// 	label: '通话结束后小休',
					// 	i18nKey: 'endSession-otherWork',
					// 	status: 'off'
					// },
					{
						label: '超时未接听后小休',
						i18nKey: 'timeout-otherWork',
						status: 'off'
					}
				],
				i18nKey: 'auto-otherWork', // 系统语言设置
				visible: true
			},
			{
				id: 5,
				label: '开启悬浮框提醒',
				children: [],
				i18nKey: 'open-float-alert', // 开启悬浮框提醒
				visible: true
			}
		];

		this.loginData = {
			username: '',
			password: '',
			org: ''
		};

		this.pocSetting = {
			autoAnswer: false,
			autoReady: false,
			autoCheckin: false
		};

		this.backgroundImageList = [];
		this.watermarkImageList = [];

		const {
			deviceConfigHidden,
			virtualBackgroundConfigHidden,
			watermarkConfigHidden,
			languageConfigHidden,
			acceptCallConfigHidden,
			floatAlertConfigHidden,
			managementMenuHidden,
			agentManagemenMenuHidden,
			userInfoHidden,
			chatBoxHidden,
			layoutConfigHidden,
			bizPannelHidden,
			qualityTestingResultHidden,
			isImagePreviewHidden,
			isCallProgressHidden,
			ignoreTools,
			headerMenus,
			isRecordHidden,
			isNotifyCustomerMediaUpgrade,
			disableAudioAnswerToVideoCall
		} = defaultPreferenceSettings;
		this.setPreferenceSettings({
			deviceConfigHidden,
			virtualBackgroundConfigHidden,
			watermarkConfigHidden,
			languageConfigHidden,
			acceptCallConfigHidden,
			floatAlertConfigHidden,
			managementMenuHidden,
			agentManagemenMenuHidden,
			userInfoHidden,
			chatBoxHidden,
			layoutConfigHidden,
			bizPannelHidden,
			qualityTestingResultHidden,
			isImagePreviewHidden,
			isCallProgressHidden,
			ignoreTools,
			headerMenus,
			isRecordHidden,
			isNotifyCustomerMediaUpgrade,
			disableAudioAnswerToVideoCall
		});

		this.setOtherWork(defaultOtherWorkParams as IOtherWork);

		this.setUserSettings(defaultUserSettings);
	}

	public setLayout(layout: ILayout) {
		this.layout = layout;
	}

	public setServiceUrl(url: string) {
		if (!url || url.trim() === '') return;
		this.serviceUrl = url;
	}

	public setManagementPath(path: string) {
		this.managementPath = path;
	}

	public setPreferenceSettings(preferenceSettings: IPreferenceSettings) {
		this.preferenceSettings = { ...this.preferenceSettings, ...preferenceSettings };
	}

	public setLoginData(loginData: ILoginData) {
		this.loginData = { ...this.loginData, ...loginData };
	}

	public setPocSetting(pocSetting: IPocSetting) {
		this.pocSetting = { ...this.pocSetting, ...pocSetting };
	}

	public setWhiteBoardInfo(info: WhiteBoardInfo) {
		this.whiteboardInfo = info;
	}

	public setDefaultMediaOptions(mediaOptions: IDefaultMediaOptions) {
		this.defaultMediaOptions = { ...this.defaultMediaOptions, ...mediaOptions };
	}

	public setCurrentTab(tab: ICurrentTab) {
		this.currentTab = tab;
	}

	public setOtherWork(otherWork: IOtherWork) {
		this.otherWork = { ...this.otherWork, ...otherWork };
	}

	public setUserSettings(userSettings: any) {
		this.userSettings = userSettings;
	}

	/** 初始化背景图片 */
	public setBackgroundImageList(backgroundImageList: IBackgroundList[]) {
		if (this.backgroundImageList.length !== 0) {
			this.backgroundImageList = [];
		}
		this.backgroundImageList = [...this.backgroundImageList, ...backgroundImageList];
	}

	/** 初始化设置水印图片 */
	public setWatermarkImageList(watermarkImageList: IWatermarkImageList[]) {
		this.watermarkImageList = [...this.watermarkImageList, ...watermarkImageList];
	}

	/** 设置背景图片默认选中ID */
	public setBackgroundID(backgroundID: number) {
		this.backgroundID = backgroundID;
	}

	/** 设置水印图片默认选中ID */
	public setWatermarkID(watermarkID: number) {
		this.watermarkID = watermarkID;
	}

	/** 设置是否开启场景转接 */
	public setTransferScene(status: boolean) {
		this.isTransferScene = status;
	}

	/** 设置是否开启实时质检结果 */
	// public setQualityTestingResultView(status: boolean) {
	// 	this.showQualityTestingResultView = status;
	// }

	/** 忽略用户变量 */
	public setIgnoreVariableLinkedKeys(keys: TIgnoreLinkedKey[]) {
		this.ignoreVariableLinkedKeys = keys;
	}

	public setLanguage(language: 'zh' | 'zh-TW' | 'en-US') {
		this.language = language;
	}

	public getLanguage() {
		return this.language;
	}

    public setAwsFileUrl(url: string) {
        if (!url || url.trim() === '') return;
		this.awsFileUrl = url;
	}
	public getAwsFileUrl() {
		return this.awsFileUrl;
	}

	/** 替换静态资源地址 */
	public replaceStaticUrl(url: string) {
		if (!url) return;
		if (typeof url !== 'string') return url;
		let str = url;
		if (this.resourceOriginUrlDomain && this.resourceReplaceUrlDomain && url.includes(this.resourceOriginUrlDomain)) {
			str = url.replace(this.resourceOriginUrlDomain, this.resourceReplaceUrlDomain);
		}
		if (typeof this.urlMapping === 'undefined') return str;
		if (!this.urlMapping || Object.keys(this.urlMapping).length === 0) return str;
		Object.keys(this.urlMapping).forEach(key => {
			if (url.includes(key)) {
				str = url.replace(key, this.urlMapping[key]);
			}
		});

		return str;
	}

	/** 设置Countly参数
	 * @param({appkey:string, host: string}) param - Countly host & app key
	 */
	public setCountlyParam(param: { appkey: string; host: string }) {
		clog.init(param.appkey, param.host);
	}

	/** set customerUrl */
	public setCustomerUrl(url: string) {
		if (!url || url.trim() === '') return;
		this.customerUrl = url;
	}

    /**
     * set publickey
     * @param key
     */
    public setPublicKey(key: string){
        this.publicKey = key;
    }

    public getPublicKey(){
        return this.publicKey;
    }

	/** set formUrl */
	public setFormUrl(url: string) {
		if (!url || url.trim() === '') return;
		this.formUrl = url;
	}

	/** set resourceOriginUrlDomain */
	public setResourceOriginUrlDomain(url: string) {
		if (!url || url.trim() === '') return;
		this.resourceOriginUrlDomain = url;
	}

	/** set resourceReplaceUrlDomain */
	public setResourceReplaceUrlDomain(url: string) {
		if (!url || url.trim() === '') return;
		this.resourceReplaceUrlDomain = url;
	}

	/** set urlMapping */
	public setUrlMapping(urlMapping: { [key: string]: string }) {
		if (!urlMapping || Object.keys(urlMapping).length === 0) return;
		this.urlMapping = urlMapping;
	}

	/** set maxUploadFileSize */
	public setMaxUploadFileSize(size: number) {
		if (!size) return;
		this.maxUploadFileSize = size;
	}

}

export default Configure;
